package com.ihd.ihardware.common.widget;

import android.app.Activity;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.pojo.MembersRes;

import java.util.List;


public class MemberDialogListAdapter extends RecyclerView.Adapter<MemberDialogListAdapter.MyHolder> {

    private List<MembersRes.DataBean> mData;
    private Activity mActivity;
    private OnItemClickListener mListener;

    public MemberDialogListAdapter(Activity mActivity, List<MembersRes.DataBean> mData) {
        this.mData = mData;
        this.mActivity = mActivity;
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, MembersRes.DataBean data);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_pop_member_item, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (position != mData.size() - 1) {
            if (!TextUtils.isEmpty(mData.get(position).getAvatar()))
                Glide.with(mActivity)
                        .load(Uri.parse(mData.get(position).getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(holder.head);
            holder.nickname.setText(mData.get(position).getMember());
            holder.nickname.setVisibility(View.VISIBLE);
            holder.border.setVisibility(View.VISIBLE);
        } else {
            holder.nickname.setVisibility(View.GONE);
            holder.border.setVisibility(View.GONE);
            holder.head.setImageResource(R.drawable.icon_cut_add);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onItemClick(holder.itemView, mData.get(position));
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        public ImageView head;
        public TextView nickname;
        private View border;

        public MyHolder(View itemView) {
            super(itemView);
            head = itemView.findViewById(R.id.head);
            nickname = itemView.findViewById(R.id.nick);
            border = itemView.findViewById(R.id.border);
        }
    }
}
